#include "BookingModule.h"

BookingModule::BookingModule()
{
	
}

void BookingModule::setup()
{

}

void BookingModule::readBookingData()
{
	vector<string> usr_str;
	string tmp_str = "";
	vector<string> vec_str;

	ifstream userFile("BOOKING.dat", ios::in);

	if(userFile.is_open()) {
		while(userFile.good()) {
			getline(userFile, tmp_str);
			if(tmp_str == "") {
				break;
			}

			usr_str.push_back(tmp_str);

		}
	} else {
		cout << "Error opening user file" << endl;
	}

	for(vector<int>::size_type i = 0; i != usr_str.size(); i++) {
		stringstream ss(usr_str[i]);
		tmp_str = "";

		vec_str.clear();
		while(getline(ss, tmp_str, ',')) {
			vec_str.push_back(tmp_str);
		}

		booking_list.push_back(Booking(vec_str[0], vec_str[1], vec_str[2], vec_str[3],
			vec_str[4], vec_str[5], vec_str[6], vec_str[7], vec_str[8], vec_str[9],
			vec_str[10], vec_str[11], vec_str[12]));

	}

	userFile.close();


	/* debug */

}

void BookingModule::writeBookingData()
{
	ofstream fileStream("BOOKING.dat", ios::out);

	if (fileStream.is_open()) {

		for (vector<int>::size_type i = 0; i < booking_list.size(); i++) {
			fileStream << booking_list[i].getBookingRef() << "," << booking_list[i].getFName() << ","
						<< booking_list[i].getLName() << "," << booking_list[i].getTitle() 
						<< "," << booking_list[i].getAddress() << "," << booking_list[i].getPostalCode() 
						<< "," << booking_list[i].getDestination() << "," << booking_list[i].getHolidayPackage()
						<< "," << booking_list[i].get1stChoice() << "," << booking_list[i].get2ndChoice() 
						<< "," << booking_list[i].getRemark() << "," << booking_list[i].getBookingDate() 
						<< "," << booking_list[i].getBookingStatus()<< endl;
		}

	} else {
		cout << "Unable to open data file" << endl;
	}

	fileStream.close();
}

void BookingModule::readDestinationData()
{
	vector<string> usr_str;
	string tmp_str = "";
	vector<string> vec_str;

	ifstream userFile("DESTINATION.dat", ios::in);

	if(userFile.is_open()) {
		while(userFile.good()) {
			getline(userFile, tmp_str);
			if(tmp_str == "") {
				break;
			}

			usr_str.push_back(tmp_str);

		}
	} else {
		cout << "Error opening user file" << endl;
	}

	for(vector<int>::size_type i = 0; i != usr_str.size(); i++) {
		stringstream ss(usr_str[i]);
		tmp_str = "";

		vec_str.clear();
		while(getline(ss, tmp_str, ',')) {
			vec_str.push_back(tmp_str);
		}

		destination_list.push_back(Destination(vec_str[0], vec_str[1]));

	}

	userFile.close();
}

void BookingModule::readPackageData()
{
	vector<string> usr_str;
	string tmp_str = "";
	vector<string> vec_str;

	ifstream userFile("PACKAGE.dat", ios::in);

	if(userFile.is_open()) {
		while(userFile.good()) {
			getline(userFile, tmp_str);
			if(tmp_str == "") {
				break;
			}

			usr_str.push_back(tmp_str);

		}
	} else {
		cout << "Error opening user file" << endl;
	}

	for(vector<int>::size_type i = 0; i != usr_str.size(); i++) {
		stringstream ss(usr_str[i]);
		tmp_str = "";

		vec_str.clear();
		while(getline(ss, tmp_str, ',')) {
			vec_str.push_back(tmp_str);
		}

		package_list.push_back(Package(vec_str[0], vec_str[1], vec_str[2]));

	}

	userFile.close();
}

void BookingModule::createBooking()
{
	string tmp;
	ClientDetail client;
	Destination dest;
	Package holiday;
	Booking newBooking;



	string refNum = getNewRefNumber();
	string fname, lname, address, postalcode, title, destination, package = "";


	cout << "*** Creating new Booking ***" << endl;
	cout << "Please enter your First Name: ";
	cin >> fname;
	cin.ignore();
	//client.setCustomerFName(tmp);
	cout << "Please enter your Last name: ";
	cin >> lname;
	cin.ignore();
	//client.setCustomerLName(tmp);
	cout << "Please enter your Address: ";
	getline(cin, address);
	//client.setCustomerAddress(tmp);
	cout << "Please enter your Postal Code: ";
	cin >> postalcode;
	cin.ignore();
	//client.setCustomerPostalCode(tmp);
	cout << "Please enter your preferred Title: ";
	cin >> title;
	cin.ignore();
	//client.setCustomerTitle(tmp);

	cout << "Please select your prefered destination: " << endl;
	int count = 0;
	for(vector<Destination>::iterator it = destination_list.begin(); it != destination_list.end(); ++it) {
		count++;
		cout << count << ". " << (*it).getDestinationName() << endl;
	}
	int opt;
	cout << "Option: ";
	cin >> opt;
	cin.ignore();
	if (opt > 0 && opt <= count) {
		dest.setDestinationId(numToString(opt));
		for(vector<Destination>::iterator it = destination_list.begin(); it != destination_list.end(); ++it) {
			if ( (*it).getDestinationId() == numToString(opt) ) {
				//dest.setDestinationName( (*it).getDestinationName() );
				destination = (*it).getDestinationId();
			}
		}
	}

	cout << "Please select prefered Holiday Package" << endl;
	count = 0;
	for(vector<Package>::iterator it = package_list.begin(); it != package_list.end(); ++it) {
		count++;
		cout << count << ". " << (*it).getPackName() << endl;;
	}

	opt = 0;
	cout << "Option: ";
	cin >> opt;
	cin.ignore();

	if (opt > 0 && opt <= count) {
		holiday.setPackId(opt);
		for(vector<Package>::iterator it = package_list.begin(); it != package_list.end(); ++it) {
			if ( (*it).getPackId() == numToString(opt) ) {
				//holiday.setPackName( (*it).getPackName() );
				//holiday.setPackDuration( (*it).getPackDuration() );
				package = (*it).getPackId();
			}
		}
	}

	booking_list.push_back(Booking(refNum, fname, lname, title, address, postalcode, destination, package,
									"01-01-2014", "01-01-2014", "NIL", "01-01-2014", "0"));

}

void BookingModule::editBooking(string uname, string urole)
{
	string inRef = "";
	string newIn = "";
	bool refValid = false;
	cout << "Please enter a Booking Reference Number: ";
	cin >> inRef;
	cin.ignore();

	string tmp = "";

	for(vector<Booking>::iterator it = booking_list.begin(); it != booking_list.end(); ++it) {
		if( (*it).getBookingRef() == inRef ) {
			int usrChoice;
			cout << "Record Found... Please update" << endl;
			cout << "1. First name" << endl;
			cout << "2. Last name" << endl;
			cout << "3. Title" << endl;
			cout << "4. Address" << endl;
			cout << "5. Postal code" << endl;
			cout << "6. Destination" << endl;
			cout << "7. Holiday package" << endl;
			cout << "Please choose an option: ";
			cin >> usrChoice;
			cin.ignore();

			switch(usrChoice) {
				case 1:
					cout << "Enter new First Name: ";
					cin >> newIn;
					cin.ignore();
					(*it).setFName(newIn);
					break;
				case 2:
					cout << "Enter new Last Name: ";
					cin >> newIn;
					cin.ignore();
					(*it).setLname(newIn);
					break;
				case 3:
					cout << "Enter new Title: ";
					cin >> newIn;
					cin.ignore();
					(*it).setTitle(newIn);
					break;
				case 4:
					cout << "Enter new Address: ";
					cin >> newIn;
					cin.ignore();
					(*it).setAddress(newIn);
					break;
				case 5:
					cout << "Enter new Postal Code: ";
					cin >> newIn;
					cin.ignore();
					(*it).setPostalCode(newIn);
					break;
				case 6:
					cout << "Enter new Destination: ";
					cin >> newIn;
					cin.ignore();
					(*it).setDestination(newIn);
					break;
				case 7:
					cout << "Enter new Holiday Package: ";
					cin >> newIn;
					cin.ignore();
					(*it).setHolidayPackage(newIn);
					break;
				default:
					break;
			}

			(*it).setBookingStatus(numToString(7));
		}
	}
}

void BookingModule::cancelBooking(string uname, string urole)
{
	string inRef = "";
	cout << "Please enter a Booking Reference Number: ";
	cin >> inRef;
	cin.ignore();

	for(vector<Booking>::iterator it = booking_list.begin(); it != booking_list.end(); ++it) {
		if( (*it).getBookingRef() == inRef ) {
			(*it).setBookingStatus("6");
		}
	}
}

void BookingModule::listAllBooking()
{
	cout << "Booking Reference\t" <<  "First Name\t" << "Last Name\t" << "Address\t" << "Postal Code\t" <<
	 "Destination\t" << "Holiday Package\t" << "Status" << endl;
	for(vector<Booking>::iterator it = booking_list.begin(); it != booking_list.end(); ++it) {
		cout << (*it).getBookingRef() << "\t" << (*it).getFName() << "\t" << (*it).getLName() << "\t" << (*it).getTitle()
		 << "\t" << (*it).getAddress() << "\t" << (*it).getPostalCode() << "\t" << (*it).getDestination()
		 << "\t" << (*it).getHolidayPackage() << "\t" << (*it).getBookingStatus() << endl;
	}
}

string BookingModule::getNewRefNumber()
{
	string tmp = booking_list.back().getBookingRef();
	int newRef = atoi(tmp.c_str());
	newRef++;
	return numToString(newRef);
}

string BookingModule::numToString(int num)
{
	string Result;          // string which will contain the result
	ostringstream convert;   // stream used for the conversion
	convert << num;      // insert the textual representation of 'Number' in the characters in the stream
	Result = convert.str(); // set 'Result' to the contents of the stream
	return Result;
}


void BookingModule::checkBooking()
{
	string inRef, inStatus, status = "";

	for(vector<Booking>::iterator it = booking_list.begin(); it != booking_list.end(); ++it) {
		if( (*it).getBookingStatus() == "0" || (*it).getBookingStatus() == "1" || (*it).getBookingStatus() == "7" ) {
			cout << (*it).getBookingRef() << "\t" << (*it).getFName() << "\t" << (*it).getLName() << "\t" << (*it).getTitle()
			 << "\t" << (*it).getAddress() << "\t" << (*it).getPostalCode() << "\t" << (*it).getDestination()
			 << "\t" << (*it).getHolidayPackage() << "\t" << (*it).getBookingStatus() << endl;
		}
	}

	cout << "Enter Reference Number: ";
	cin >> inRef;
	cin.ignore();
	cout << "Please select which status to set" << endl;
	cout << "1. Incomplete / Invalid booking form" << endl;
	cout << "2. Booking Checked" << endl;
	cin >> inStatus;
	cin.ignore();

	for(vector<Booking>::iterator it = booking_list.begin(); it != booking_list.end(); ++it) {
		if( (*it).getBookingRef() == "inRef" ) {
			(*it).setBookingStatus(inStatus);
		}
	}

}

void BookingModule::vetBooking()
{
	string inRef, inStatus, status = "";

	for(vector<Booking>::iterator it = booking_list.begin(); it != booking_list.end(); ++it) {
		if( (*it).getBookingStatus() == "2" || (*it).getBookingStatus() == "3"  ) {
			cout << (*it).getBookingRef() << "\t" << (*it).getFName() << "\t" << (*it).getLName() << "\t" << (*it).getTitle()
			 << "\t" << (*it).getAddress() << "\t" << (*it).getPostalCode() << "\t" << (*it).getDestination()
			 << "\t" << (*it).getHolidayPackage() << "\t" << (*it).getBookingStatus() << endl;
		}
	}

	cout << "Enter Reference Number: ";
	cin >> inRef;
	cin.ignore();
	cout << "Please select which status to set" << endl;
	cout << "1. Waiting List" << endl;
	cout << "2. Payment Required" << endl;
	cin >> inStatus;
	cin.ignore();

	if(inStatus == "1") {
		status = "3";
	} else if (inStatus == "2") {
		status = "4";
	}

	for(vector<Booking>::iterator it = booking_list.begin(); it != booking_list.end(); ++it) {
		if( (*it).getBookingRef() == "inRef" ) {
			(*it).setBookingStatus(status);
		}
	}


}

void BookingModule::confirmBooking()
{
	string inRef, inStatus, status = "";

	for(vector<Booking>::iterator it = booking_list.begin(); it != booking_list.end(); ++it) {
		if( (*it).getBookingStatus() == "4" ) {
			cout << (*it).getBookingRef() << "\t" << (*it).getFName() << "\t" << (*it).getLName() << "\t" << (*it).getTitle()
			 << "\t" << (*it).getAddress() << "\t" << (*it).getPostalCode() << "\t" << (*it).getDestination()
			 << "\t" << (*it).getHolidayPackage() << "\t" << (*it).getBookingStatus() << endl;
		}
	}

	cout << "Enter Reference Number: ";
	cin >> inRef;
	cin.ignore();
	cout << "Please select which status to set" << endl;
	cout << "1. Booking Confirmed" << endl;
	cout << "2. Booking Cancelled" << endl;
	cin >> inStatus;
	cin.ignore();

	if(inStatus == "1") {
		status = "5";
	} else if (inStatus == "2") {
		status = "6";
	}

	for(vector<Booking>::iterator it = booking_list.begin(); it != booking_list.end(); ++it) {
		if( (*it).getBookingRef() == "inRef" ) {
			(*it).setBookingStatus(status);
		}
	}
}